<template>
  <div class="app-container">
    <el-card shadow="never" class="mb20">
      <el-row>
        <el-col>
          <label class="radio-label">是否已读：</label>
          <el-radio-group v-model="listQuery.notifyStatus" @change="handleFilter">
            <el-radio label>全部</el-radio>
            <el-radio label="UNREAD">未读</el-radio>
            <el-radio label="READED">已读</el-radio>
          </el-radio-group>
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            style="margin-left: 15px;"
            @click="handleFilter"
          ></el-button>
        </el-col>
      </el-row>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      row-key="id"
      :max-height="height"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column prop="proInsName" label="相关流程" min-width="220" align="center"></el-table-column>
      <el-table-column prop="processStatus" label="流程状态" min-width="100" align="center">
        <template slot-scope="scope">{{ scope.row.processStatus | WORKFLOW_PROCESS_STATUS }}</template>
      </el-table-column>
      <el-table-column prop="startUserName" label="转发人" min-width="120" align="center"></el-table-column>
      <el-table-column prop="notifyStatus" label="是否已读" min-width="100" align="center">
        <template slot-scope="scope">{{ scope.row.notifyStatus | WORKFLOW_NOTIFY_STATUS}}</template>
      </el-table-column>
      <el-table-column align="center" label="转发时间" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | moment('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="80" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            circle
            icon="el-icon-view"
            title="查看"
            @click="handleView(scope.row)"
          ></el-button>
          <el-button
            v-if="Object.keys(WORKFLOW_NOTIFY_STATUS)[1] === scope.row.notifyStatus"
            type="primary"
            size="mini"
            circle
            icon="el-icon-check"
            title="标记为已读"
            @click="handleReaded(scope.row, true)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      class="fr"
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import enums from '@/utils/enums'
import Pagination from '@/views/workflow/components/Pagination'
import { queryNotifyProcess, readProcessNotify } from '@/api/workflow/bpmn/runtime'
export default {
  props: ['height'],
  components: {
    Pagination
  },
  data() {
    return {
      total: 0,
      listQuery: {
        notifyStatus: 'UNREAD',
        pageNo: 1,
        pageSize: 10
      },
      list: [],
      listLoading: false
    }
  },
  filters: {
    WORKFLOW_NOTIFY_STATUS(val) {
      return enums.WORKFLOW_NOTIFY_STATUS[val]
    },
    WORKFLOW_PROCESS_STATUS(val) {
      return enums.WORKFLOW_PROCESS_STATUS[val]
    }
  },
  computed: {
    WORKFLOW_NOTIFY_STATUS() {
      return enums.WORKFLOW_NOTIFY_STATUS
    }
  },
  created() {},
  methods: {
    handleFilter() {
      this.total = 0
      this.listQuery.pageNo = 1
      this.getList()
    },
    getList() {
      // 转发通知
      this.listLoading = true
      const params = {
        ...this.listQuery
      }
      queryNotifyProcess(params).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleReaded(row, read) {
      // 标记转发为已读
      const data = {
        processInstanceId: row.proInsId
      }
      readProcessNotify(data).then(res => {
        if (read) {
          this.$message.success('标记成功')
        }
        this.queryNotifyProcess()
      })
    },
    handleView(row, notify) {
      this.handleReaded(row) // 点击查看的时候标记已读
      this.$router.push({
        path: '/workflow/bpmn/process',
        query: {
          procdefId: row.processDefinitionId,
          deploymentId: row.deploymentId,
          processInstanceId: row.proInsId,
          taskId: row.taskId,
          layoutId: row.formLayoutId,
          notify: true,
          readonly: 1
        }
      })
    }
  }
}
</script>

<style scoped>
.tree-box {
  height: 200px;
  overflow: auto;
}
</style>
